<?php

class Backend_VenueController extends Zend_Controller_Action
{

    protected $_redirector = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('backend/layout');
        $this->view->title = "";
        $this->view->venuectrl = true;

        $this->_redirector = $this->_helper->getHelper("Redirector");

        // check session
        session_start();
        if(!isset($_SESSION['admin'])){
            $this->_redirector->gotoUrl('/backend/auth/login');
        }
        else{
            //role must be admin
            if($_SESSION['admin']['role_id']>2){
                $this->_redirector->gotoUrl('/backend/');
            }

            $this->view->session = $_SESSION['admin'];
        }

        $floor_db = new Application_Model_DbTable_Floor();
        $this->view->floors = $floor_db->getAll();
    }

    public function indexAction()
    {
        $fid = $this->getRequest()->getParam("fid", 0);

        if($fid > 0){
            $floor_db = new Application_Model_DbTable_Floor();
            $this->view->floor = $floor_db->getFloor($fid);
            $this->view->title = $this->view->floor['name'];
        }
           
        $venue_db = new Application_Model_DbTable_Venue();
        $this->view->venues = $venue_db->getVenues($fid);
    }

    public function uploadAction()
    {
        $this->_helper->layout->disableLayout();

        $type = $this->getRequest()->getParam('type','notype');
        $id = $this->getRequest()->getParam('id',0);

        $form = new Backend_Form_Upload();
        
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            if(strcmp($type,"floor")==0){ //floor
                $dir = realpath('./images/floors').DIRECTORY_SEPARATOR;
                $filename = "floor_".$id.".jpg";
            }
            else{ //venue
                $dir = realpath('./images/venues').DIRECTORY_SEPARATOR;
                
                if(strcmp($type,"venue1")==0) $filename = "venue_".$id."_main.jpg";
                else if(strcmp($type,"venue2")==0) $filename = "venue_".$id."_1.jpg";
                else if(strcmp($type,"venue3")==0) $filename = "venue_".$id."_2.jpg";
                else if(strcmp($type,"venue4")==0) $filename = "venue_".$id."_3.jpg";
            }

            $form->file->setDestination($dir);
            $form->file->addFilter("Rename",array('target'=>$dir.$filename,'overwrite'=>true),$form->upload);
            $form->file->receive();

            if(strcmp($type,"floor")==0){
                $this->_helper->getHelper("Redirector")->gotoUrl("backend/venue/index/fid/$id");
            }
            else{
                $this->_helper->getHelper("Redirector")->gotoUrl("backend/venue/details/vid/$id");
            }
        }

        $this->view->form = $form;
        $this->view->type = $type;
        $this->view->id = $id;
    }

    public function addfloorAction()
    {
        $this->_helper->layout->disableLayout();
        $this->view->title = "Add Floor";

        $form = new Backend_Form_FloorAdd();

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            if($form->isValid($postData)){
                $floor_db = new Application_Model_DbTable_Floor();
                $floor_db->insertFloor($postData);

                $this->_redirector->gotoUrl("/backend/venue/");
            }
            else{
                $form->populate($postData);
            }
        }

        $this->view->form = $form;
    }

    public function addvenueAction()
    {
        $this->_helper->layout->disableLayout();
        $this->view->title = "Add Venue";

        $fid = $this->getRequest()->getParam('fid',0); //floor id
        if($fid==0){
            $this->_redirector->gotoUrl('/backend/venue/');
        }

        $form = new Backend_Form_VenueAdd();

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            if($form->isValid($postData)){
                $postData['floor_id'] = $fid;

                $venue_db = new Application_Model_DbTable_Venue();
                $venue_db->insertVenue($postData);

                $this->_redirector->gotoUrl("/backend/venue/index/fid/$fid");
            }
            else{
                $form->populate($postData);
            }
        }

        $this->view->form = $form;
        $this->view->fid = $fid;
    }

    public function detailsAction()
    {
        $vid = $this->getRequest()->getParam('vid',0);

        if($vid>0){
            $venue_db = new Application_Model_DbTable_Venue();
            $this->view->details = $venue_db->getVenue($vid);
        }
        else{
            $this->_redirector->gotoUrl('/backend/venue');
        }
    }

    public function editAction()
    {
        $this->_helper->layout->disableLayout();

        $fid = $this->getRequest()->getParam('fid');
        $vid = $this->getRequest()->getParam('vid');

        // Get post parameter (edit value)
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            if(!is_null($fid)){
                $floor_db = new Application_Model_DbTable_Floor();
                $floor_db->updateFloor($fid, $postData);

                $this->_redirector->gotoUrl('/backend/venue/index/fid/'.$fid);
            }
            else if(!is_null($vid)){
                $venue_db = new Application_Model_DbTable_Venue();
                $venue_db->updateVenue($vid, $postData);

                $this->_redirector->gotoUrl('/backend/venue/index/fid/'.$postData['floor_id']);
            }
        }

        // Initialize form
        if(!is_null($fid)){
            $floor_db = new Application_Model_DbTable_Floor();
            $form = new Backend_Form_FloorAdd();

            $form->populate($floor_db->getFloor($fid)->toArray());
            $this->view->title = "Edit Floor";
            $this->view->param = "fid/$fid";
        }
        else if(!is_null($vid)){
            $venue_db = new Application_Model_DbTable_Venue();
            $form = new Backend_Form_VenueAdd();

            $form->populate($venue_db->getVenue($vid)->toArray());
            $this->view->title = "Edit Venue";
            $this->view->param = "vid/$vid";
        }

        $this->view->form = $form;
    }

    public function deleteAction()
    {
        // Get post parameter (delete value)
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            if(!is_null($postData['fid'])){
                $floor_db = new Application_Model_DbTable_Floor();
                $floor_db->deleteFloor($postData['fid']);

                $this->_redirector->gotoUrl('/backend/venue/index/');
            }
            else if(!is_null($postData['vid'])){
                $venue_db = new Application_Model_DbTable_Venue();
                $venue_db->deleteVenue($postData['vid']);

                $this->_redirector->gotoUrl('/backend/venue/index/fid/'.$postData['floor_id']);
            }
        }
    }
}













